package com.yx.cloud.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.product.sweeper.bean.RobotStatus;
import com.yx.cloud.R;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.view.BatteryViewStatic;

import org.json.JSONObject;

import java.util.List;

public class DeviceListDataAdapter extends RecyclerView.Adapter implements View.OnClickListener, View.OnLongClickListener {
    private final static String TAG = "DeviceListDataAdapter";
    private Context mContext;
    private List<Robot> beanList;
    private OnItemClickListener mOnItemClickListener = null;
    private onChecked checked;
    private JSONObject productConfig;
    private boolean showSelect = false;

    public DeviceListDataAdapter(Context context, List list) {
        LogUtils.i("设备列表 --DeviceListDataAdapter----------" + list.size());
        mContext = context;
        beanList = list;
    }

    public onChecked getChecked() {
        return checked;
    }

    public void setChecked(onChecked checked) {
        this.checked = checked;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LogUtils.i("设备列表 --onCreateViewHolder----------");
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.device_list_data_adapter_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {

        LogUtils.i("设备列表 --onBindViewHolder----------");
        Robot bean = beanList.get(position);
        //LogUtils.i(position + " 设备列表 数据: " + new Gson().toJson(bean));
        productConfig = bean.getConfig();
        if (("x5-max").equals(bean.getProductId())) {
            ((ViewHolder) holder).ivRobotIcon.setImageResource(R.mipmap.x5_evo_base_station_icon);
        } else {
            ((ViewHolder) holder).ivRobotIcon.setImageResource(productConfig.optInt(ProductConfigConstants.robot_airview_default));
        }

        ((ViewHolder) holder).tvRobotName.setText(bean.getName());
        boolean online = bean.isOnline();
        if (!online) {
            ((ViewHolder) holder).rlFragmentBattery.setVisibility(View.INVISIBLE);//电量布局
            ((ViewHolder) holder).tvState.setText(mContext.getString(R.string.H_State_Offline));
            ((ViewHolder) holder).mBtnRecharge.setBackgroundResource(R.mipmap.bottom_tab_start_recharge_icon_diable);
            ((ViewHolder) holder).mBtnClean.setBackgroundResource(R.mipmap.bottom_tab_start_clean_icon_disable);
            ((ViewHolder) holder).mBtnRecharge.setEnabled(false);
            ((ViewHolder) holder).mBtnClean.setEnabled(false);
        } else {
            ((ViewHolder) holder).rlFragmentBattery.setVisibility(View.VISIBLE);//电量布局
            ((ViewHolder) holder).mBtnRecharge.setEnabled(true);
            ((ViewHolder) holder).mBtnClean.setEnabled(true);
            showState(bean, ((ViewHolder) holder).tvState, ((ViewHolder)
                            holder).mBtnRecharge, ((ViewHolder) holder).mBtnClean,
                    ((ViewHolder) holder).mIvChargringIcon);
        }
        int battery = bean.getRobotStatus().getBattery();
        ((ViewHolder) holder).mTvPower.setText(battery + "%");
        ((ViewHolder) holder).mBatteryView.setPower(battery, bean);

        ((ViewHolder) holder).mBtnRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.setRechargeCommClick(position);
            }
        });
        ((ViewHolder) holder).mBtnClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.setCleanCommClick(position);
            }
        });
        ((ViewHolder) holder).ivSelect.setOnCheckedChangeListener(null);//清掉监听器
        ((ViewHolder) holder).ivSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    LogUtils.i(position + " : MessageCenterActivity onCheckedChanged : " + isChecked);
                    checked.CheckedChanged(position, isChecked, buttonView); //给按钮设置回调
                }
            }
        });
        if (showSelect) {
            ((ViewHolder) holder).ivSelect.setVisibility(View.VISIBLE);
        } else {
            ((ViewHolder) holder).ivSelect.setVisibility(View.GONE);
        }
        holder.itemView.setTag(position);
    }

    public void updateData(List<Robot> list, boolean showSelect) {
        beanList = list;
        this.showSelect = showSelect;
        notifyDataSetChanged();
    }

    public void showSelect(boolean showSelect) {
        this.showSelect = showSelect;
        notifyDataSetChanged();
    }

    public boolean isShowSelect() {
        return showSelect;
    }

    public void updateData(List<Robot> list) {
        beanList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Item点击事件方法
     * <p>
     * //     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        mOnItemClickListener.onLongClick(v, (Integer) v.getTag());
        return true;
    }

    private void showState(Robot bean, TextView tvState, Button mBtnRecharge, Button mBtnClean, ImageView mIvChargringIcon) {
        int mRechargeIcon = 0, mCleanIcon = 0;

        int state = bean.getRobotStatus().getWorkState();
        int subState = bean.getRobotStatus().getSubState();
        int mode = bean.getRobotStatus().getWorkMode();
        mIvChargringIcon.setVisibility(View.GONE);
        if (state == RobotStatus.STATE_DORMANT) {
            LogUtils.i("state ==5: ");
            //休眠 状态
            tvState.setText(mContext.getString(R.string.H_State_Sleep));
            mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
            mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
        } else if (state == RobotStatus.STATE_STANDBY) {
            LogUtils.i("state ==0: ");
            tvState.setText(mContext.getString(R.string.H_State_Stanby));
            mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
            mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
        } else if (state == RobotStatus.STATE_WORKING) {
            if (subState == RobotStatus.SUBSTATE_NONE) {
                //子状态为空 说明是清扫中
                if (mode == RobotStatus.MODE_AUTO_CLEAN) {
                    LogUtils.i("state ==1: ");
                    //工作中 状态 全屋清扫
                    tvState.setText(mContext.getString(R.string.U_AutoClean));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_pause_clean_icon;
                } else if (mode == RobotStatus.MODE_ROOM_CLEAN) {
                    //选区清扫
                    tvState.setText(mContext.getString(R.string.U_RoomClean));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_pause_clean_icon;
                } else if (mode == RobotStatus.MODE_CUSTOM_CLEAN) {
                    //区域清扫/自定义清扫
                    tvState.setText(mContext.getString(R.string.U_ZoneClean));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_pause_clean_icon;
                } else if (mode == RobotStatus.MODE_EDGE_CLEAN) {
                    //沿边清扫
                    tvState.setText(mContext.getString(R.string.U_EdgeClean));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_pause_clean_icon;
                } else if (mode == RobotStatus.MODE_SPOT_CLEAN) {
                    //定点扫清扫
                    tvState.setText(mContext.getString(R.string.U_SpotClean));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_pause_clean_icon;
                } else if (mode == RobotStatus.MODE_RECHARGE) {
                    //回充中
                    tvState.setText(mContext.getString(R.string.H_State_Recharge));
                    mRechargeIcon = R.mipmap.bottom_tab_pause_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
                } else if (mode == RobotStatus.MODE_CONTROL) {
                    //遥控模式
                    tvState.setText(mContext.getString(R.string.U_RemoteMode));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
                }
            } else {
                //子状态不为空 说明显示子状态信息
                if (subState == RobotStatus.SUBSTATE_WORKING_REPOSITING) {
                    LogUtils.i("state ==22: ");
                    tvState.setText(mContext.getString(R.string.H_State_Locate));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_pause_clean_icon;
                } else if (subState == RobotStatus.SUBSTATE_WORKING_CREATINGMAP) {
                    LogUtils.i("state ==23: ");
                    tvState.setText(mContext.getString(R.string.U_NewMap));
                    mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                    mCleanIcon = R.mipmap.bottom_tab_pause_clean_icon;
                }
            }
        } else if (state == RobotStatus.STATE_PAUSE) {
            LogUtils.i("state ==2: ");
            //暂停工作状态
            tvState.setText(mContext.getString(R.string.U_Pause));
            if (mode == RobotStatus.MODE_RECHARGE) {
                mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
            } else {
                mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
                mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
            }
        } else if (state == RobotStatus.STATE_CHARGING) {
            int battery = bean.getRobotStatus().getBattery();
            String bay = "";
            LogUtils.i(battery + " : state ==3: ");
            if (battery == 100) {
                //充电完成
                bay = mContext.getResources().getString(R.string.H_State_FullCharge);
            } else {
                //充电中
                bay = mContext.getResources().getString(R.string.H_State_Charging);
                mIvChargringIcon.setVisibility(View.VISIBLE);
            }
            tvState.setText(bay);
            mRechargeIcon = R.mipmap.bottom_tab_recharge_not_clickable;
            mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
        } else if (state == RobotStatus.STATE_ERROR) {
            tvState.setText(mContext.getString(R.string.H_State_Error));
            mRechargeIcon = R.mipmap.bottom_tab_start_recharge_icon;
            mCleanIcon = R.mipmap.bottom_tab_start_clean_icon;
        }
        mBtnRecharge.setBackgroundResource(mRechargeIcon);
        mBtnClean.setBackgroundResource(mCleanIcon);
    }

    public interface onChecked {
        void CheckedChanged(int position, boolean isChecked, CompoundButton buttonView);
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onLongClick(View view, int position);

        void setRechargeCommClick(int position);

        void setCleanCommClick(int position);

    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public CheckBox ivSelect;
        public TextView tvRobotName;
        public TextView tvState, mTvPower;
        public Button mBtnRecharge, mBtnClean;
        public BatteryViewStatic mBatteryView;
        public ImageView mIvChargringIcon, ivRobotIcon;
        public LinearLayout rlFragmentBattery;

        public ViewHolder(View itemView) {
            super(itemView);
            ivRobotIcon = itemView.findViewById(R.id.iv_icon);
            tvRobotName = itemView.findViewById(R.id.tvRobotName);
            tvState = itemView.findViewById(R.id.tvState);
            ivSelect = itemView.findViewById(R.id.iv_select);
            mBtnRecharge = itemView.findViewById(R.id.btnRecharge);
            mBtnClean = itemView.findViewById(R.id.btnClean);
            mBatteryView = itemView.findViewById(R.id.batteryView);
            mTvPower = itemView.findViewById(R.id.tv_power);
            mIvChargringIcon = itemView.findViewById(R.id.iv_chargringIcon);
            rlFragmentBattery = itemView.findViewById(R.id.rl_fragmentBattery);
        }
    }

}
